package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.annotation.TingshuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @PostMapping("/uploadTrack")
    @Operation(summary = "上传声音")
    public Result uploadTrack(MultipartFile file) {

        // 文件在vod中的id mediaFileId
        // 文件在vod中的地址 mediaUrl
        Map<String, Object> map = trackInfoService.uploadTrack(file);

        return Result.ok(map);
    }

    @PostMapping("/saveTrackInfo")
    @TingshuLogin
    @Operation(summary = "保存声音")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {

        trackInfoService.saveTrackInfo(trackInfoVo);

        return Result.ok();
    }

    @PostMapping("/findUserTrackPage/{pn}/{pz}")
    @Operation(summary = "分页展示用户创作的声音列表")
    @TingshuLogin
    public Result findUserTrackPage(@PathVariable(value = "pn") Long pn, @PathVariable(value = "pz") Long pz, @RequestBody TrackInfoQuery trackInfoQuery) {
        IPage<TrackListVo> pageParam = new Page<>(pn, pz);

        trackInfoQuery.setUserId(AuthContextHolder.getUserId());
        pageParam = trackInfoService.findUserTrackPage(pageParam, trackInfoQuery);

        return Result.ok(pageParam);
    }

    @TingshuLogin
    @Operation(summary = "根据声音id查询声音对象")
    @GetMapping("/getTrackInfo/{trackId}")
    public Result getTrackInfo(@PathVariable(value = "trackId") Long trackId) {
        return Result.ok(trackInfoService.getById(trackId));
    }

    @TingshuLogin
    @PutMapping("/updateTrackInfo/{trackId}")
    @Operation(summary = "修改声音")
    public Result updateTrackInfo(@PathVariable(value = "trackId") Long trackId, @RequestBody TrackInfoVo trackInfoVo) {

        trackInfoService.updateTrackInfo(trackId, trackInfoVo);

        return Result.ok();
    }

    @Operation(summary = "根据声音id删除声音")
    @TingshuLogin
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable(value = "trackId") Long trackId) {
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }

    @GetMapping("/findAlbumTrackPage/{albumId}/{pn}/{pz}")
    @Operation(summary = "根据专辑id查询该专辑下声音列表且显示付费图标")
    @TingshuLogin(required = false)
    public Result findAlbumTrackPage(@PathVariable(value = "albumId") Long albumId, @PathVariable(value = "pn") Long pn, @PathVariable(value = "pz") Long pz) {


        IPage<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pn, pz);
        albumTrackListVoPage = trackInfoService.findAlbumTrackPage(albumTrackListVoPage, albumId);
        return Result.ok(albumTrackListVoPage);
    }

    @GetMapping(value = "/getTrackStatVo/{trackId}")
    @Operation(summary = "根据声音id查询声音的统计信息")
    public Result getTrackStatVo(@PathVariable(value = "trackId") Long trackId) {

        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);

    }

    @Operation(summary = "分集展示要买的声音列表")
    @TingshuLogin
    @GetMapping("/findUserTrackPaidList/{currentTrackId}")
    public Result findUserTrackPaidList(@PathVariable(value = "currentTrackId") Long currentTrackId) {
        List<Map<String, Object>> mapList = trackInfoService.findUserTrackPaidList(currentTrackId);
        return Result.ok(mapList);
    }
}

